#!/usr/bin/env bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

#=================================================
#	System Required: # Ubuntu 16.04
#	Description: # lxd manage
#	Version: # 1.0.0
#	Author: #
#	Blog: #
#=================================================

sh_ver="1.0.0"
filepath=$(cd "$(dirname "$0")"; pwd)

file_1=$(echo -e "${filepath}"|awk -F "$0" '{print $1}')

Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m"
Info="${Green_font_prefix}[信息]${Font_color_suffix}"
Error="${Red_font_prefix}[错误]${Font_color_suffix}"
Tip="${Green_font_prefix}[注意]${Font_color_suffix}"


check_root(){
	[[ $EUID != 0 ]] && echo -e "${Error} 当前非ROOT账号(或没有ROOT权限)，无法继续操作，请更换ROOT账号或使用 ${Green_background_prefix}sudo su${Font_color_suffix} 命令获取临时ROOT权限（执行后可能会提示输入当前账号的密码）。" && exit 1
}

check_sys(){
	if [[ -f /etc/redhat-release ]]; then
		release="centos"
	elif cat /etc/issue | grep -q -E -i "debian"; then
	# grep
	# -q supress all normal output 不输出,匹配到就返回
	# -E PATTERN is an extended regular expression 使用扩展正则表达式意味着不需要转义就能表示字符的特殊含义，包括?,+,{,|,(和)。
	# -i ignore case distinctions 不分大小写
		release="debian"
	elif cat /etc/issue | grep -q -E -i "ubuntu"; then
		release="ubuntu"
	elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then
		release="centos"
	elif cat /proc/version | grep -q -E -i "debian"; then
		release="debian"
	elif cat /proc/version | grep -q -E -i "ubuntu"; then
		release="ubuntu"
	elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then
		release="centos"
    fi
	bit=`uname -m`
}

check_installed_status(){
    type lxd &> /dev/null
	[[ ! $? ]] && echo -e "${Error} lxd 没有安装，请检查 !" && exit 1
}

Installation_dependency(){
    if [[ ${release} == "centos" ]]; then
        yum update
        yum install -y gzip
    else
        apt-get update
        apt-get install -y gzip
    fi
	\cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
	# -f, 覆盖同名文件
}

check_pid(){
	PID=`ps -ef |grep -v grep | grep $1 |awk '{print $2}'`
}

check_sys

echo && echo -e "  lxd 一键管理脚本 ${Red_font_prefix}[v${sh_ver}]${Font_color_suffix}

 ${Green_font_prefix} 0.${Font_color_suffix} 升级脚本
————————————
 ${Green_font_prefix} 1.${Font_color_suffix} 安装 lxd
 ${Green_font_prefix} 2.${Font_color_suffix} 安装 lxdui
 ${Green_font_prefix} 3.${Font_color_suffix} 更新 lxd
 ${Green_font_prefix} 4.${Font_color_suffix} 更新 lxdui
 ${Green_font_prefix} 5.${Font_color_suffix} 卸载 lxd & lxdui
————————————
 ${Green_font_prefix} 6.${Font_color_suffix} 启动 lxdui
————————————
 ${Green_font_prefix} 7.${Font_color_suffix} 设置 lxc
————————————" && echo
    type lxd &> /dev/null
	if [[ $? ]]; then
		check_pid lxd
		if [[ ! -z "${PID}" ]]; then
			echo -e " 当前状态: ${Green_font_prefix}lxd 已安装${Font_color_suffix} 并 ${Green_font_prefix}已启动${Font_color_suffix}"
		else
			echo -e " 当前状态: ${Green_font_prefix}lxd 已安装${Font_color_suffix} 但 ${Red_font_prefix}未启动${Font_color_suffix}"
		fi
	else
		echo -e " 当前状态: ${Red_font_prefix}lxd 未安装${Font_color_suffix}"
	fi
    LXDUI_FILE="${filepath}/lxdui"
    if [[ -e ${LXDUI_FILE} ]]; then
		check_pid lxdui
		if [[ ! -z "${PID}" ]]; then
			echo -e " 当前状态: ${Green_font_prefix}lxdui 已安装${Font_color_suffix} 并 ${Green_font_prefix}已启动${Font_color_suffix}"
		else
			echo -e " 当前状态: ${Green_font_prefix}lxdui 已安装${Font_color_suffix} 但 ${Red_font_prefix}未启动${Font_color_suffix}"
		fi
	else
		echo -e " 当前状态: ${Red_font_prefix}lxdui 未安装${Font_color_suffix}"
	fi
	echo
	read -e -p " 请输入数字 [0-10]:" num
	case "$num" in
		0)
		Update_Shell
		;;
		1)
		Install
		;;
		2)
		Update
		;;
		3)
		Uninstall
		;;
		4)
		Start
		;;
		5)
		Stop
		;;
		6)
		Restart
		;;
		7)
		Set
		;;
		8)
		View
		;;
		9)
		View_Log
		;;
		10)
		View_user_connection_info
		;;
		*)
		echo "请输入正确数字 [0-10]"
		;;
	esac
fi